package org.example.practiceexamples.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.example.practiceexamples.dox.Appointment;
import org.example.practiceexamples.dox.Course;
import org.example.practiceexamples.dox.User;
import org.example.practiceexamples.exception.Code;
import org.example.practiceexamples.repository.AppointmentRepository;
import org.example.practiceexamples.repository.CourseRepository;
import org.example.practiceexamples.repository.UserRepository;
import org.example.practiceexamples.service.TeacherService;
import org.example.practiceexamples.vo.ResultVO;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/teacher/")
public class TeacherController {

    private final TeacherService teacherService;
//    private final UserRepository userRepository;
//    private final CourseRepository courseRepository;
//    private final AppointmentRepository appointmentRepository;

    @PostMapping("addAppointment")
    public ResultVO addAppointment(@RequestAttribute String uid, @RequestBody Appointment appointment) {
        // 参数验证
        if (appointment == null || uid.isEmpty()) {
            return ResultVO.error(Code.ERROR, "Invalid appointment or account");
        }
        try{
            teacherService.saveAppointment(appointment);
            return ResultVO.success(appointment.getId(),"增加成功！");
        }
        catch (Exception e){
            log.error("Error saving appointment", e);
            return ResultVO.error(Code.ERROR, "Failed to save appointment");
        }
    }

    @PostMapping("addCourse")
    public ResultVO addCourse(@RequestAttribute String uid, @RequestBody Course course) {
        if (course == null || uid.isEmpty()) {
            return ResultVO.error(Code.ERROR, "Invalid course or account");
        }
        try{
            teacherService.saveCourse(course);
            return ResultVO.success(course.getId(),"增加成功！");
        }
        catch (Exception e){
            log.error("Error saving course", e);
            return ResultVO.error(Code.ERROR, "Failed to save course");
        }
    }

    @GetMapping("getCourse/{cid}")
    public ResultVO getCourse(@RequestAttribute String uid,@PathVariable String cid) {
        Course course = teacherService.findCourseById(cid);
        return ResultVO.success(course,"查询成功！");
    }

//    @GetMapping("getCourses")
//    public ResultVO getCourses(@RequestAttribute String uid) {
//        try{
//            List<Course> courses = teacherService.findCoursesByUid(uid);
//            log.debug("{}", courses);
//            return ResultVO.success(courses,"查询成功！");
//        }
//        catch (Exception e){
//            log.error("Error getting courses", e);
//            return ResultVO.error(Code.ERROR, "Failed to get courses");
//        }
//    }

    @GetMapping("getCourses")
    public ResultVO getCourses(@RequestAttribute String uid) {
        List<Course> courses = teacherService.findCoursesByUid(uid);
        return ResultVO.success(courses,"查询成功!");
    }

    @GetMapping("getAppointment/{aid}")
    public ResultVO getAppointment(@RequestAttribute String uid,@PathVariable String aid) {
        Appointment a = teacherService.findAppointmentById(aid);
        return ResultVO.success(a,"查询成功！");
    }

    @GetMapping("getAppointments")
    public ResultVO getAppointments(@RequestAttribute String uid) {
        List<Appointment> appointments = teacherService.findAllAppointmentsByUid(uid);
        return ResultVO.success(appointments,"查询成功！");
    }

    @GetMapping("getAllAppointments")
    public ResultVO getAllAppointments() {
        List<Appointment> appointments = teacherService.findAllAppointments();
        return ResultVO.success(appointments,"查询成功！");
    }

    @DeleteMapping("deleteCourse/{cid}")
    public ResultVO deleteCourse(@RequestAttribute String uid,@PathVariable String cid) {
        try {
            teacherService.deleteCourseById(uid,cid);
            return ResultVO.success("删除成功！");
        }
        catch (Exception e){
            log.error("Error deleting course", e);
            return ResultVO.error(Code.ERROR, "Failed to delete course");
        }

    }

    @DeleteMapping("deleteAppointment/{aid}")
    public ResultVO deleteAppointment(@PathVariable String aid,@RequestAttribute String uid) {
        try{
            teacherService.deleteAppointmentById(uid,aid);
            return ResultVO.success("删除成功");
        }
        catch (Exception e){
            log.error("Error saving appointment", e);
            return ResultVO.error(Code.ERROR, e.getMessage());

        }
    }

    @DeleteMapping("deleteAppointments")
    public ResultVO deleteAllAppointments(@RequestAttribute String uid) {
        List<Appointment> appointments = teacherService.findAllAppointmentsByUid(uid);
        for(Appointment appointment : appointments){
            try{
                teacherService.deleteAppointmentById(uid,appointment.getId());
            }
            catch (Exception e){
                log.error("Error saving appointment", e);
                return ResultVO.error(Code.ERROR, "Failed to delete appointment");
            }
        }
        return ResultVO.success("删除成功");
    }

    @PostMapping("updateUser")
    public ResultVO update(@RequestAttribute String uid, @RequestBody User user) {
        try{
            if(user.getDescription()!=null){
                teacherService.updateDescription(uid,user.getDescription());
            }
            if(user.getPassword()!=null){
               teacherService.updatePasswordById(uid,user.getPassword());
            }
            if(user.getProfession()!=null){
                teacherService.updateProfessionById(uid,user.getProfession());
            }
            if(user.getPhone()!=null){
                teacherService.updatePhoneById(uid,user.getPhone());
            }
            return ResultVO.success("修改成功！");
        }
        catch (Exception e){
            log.error("Error saving user", e);
            return ResultVO.error(Code.ERROR, "Failed to update user");
        }
    }


}
